package com.example.homemodule.view.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.examination.mlib.constants.ARouterConstants;
import com.examination.mlib.constants.AllStringConstants;
import com.examination.mlib.constants.ClickApi;
import com.examination.mlib.constants.ProjectConfigure;
import com.example.homemodule.R;
import com.example.homemodule.databinding.NewHomeFragmentDataBinding;
import com.example.homemodule.viewmodel.HomeViewModel;
import com.yilijk.base.base.BaseFragment;
import com.yilijk.base.utils.ClickUtils;

/**
 * Created by SHICHENG
 * <p>
 * Time on
 */
@Route(path = ARouterConstants.HOME_FRAGMENT)
public class NewHomeFragment extends BaseFragment<HomeViewModel, NewHomeFragmentDataBinding> {

    private BroadcastReceiver refreshHomeDataReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            refreshHomeData();
        }
    };

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_home_new;
    }

    @Override
    protected void processLogic(Bundle bundle) {
        ClickUtils.sendEvent("pages_index_load", getContext(), "首页载入");
        mViewModel.initModel(binding,getContext(),getActivity(),getChildFragmentManager());
        mViewModel.initView();
    }

    @Override
    protected void dispostData() {

    }

    @Override
    protected void setListener() {
        mViewModel.initListener();
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (getActivity() != null) {
            getActivity().registerReceiver(refreshHomeDataReceiver, new IntentFilter(AllStringConstants.BroadcastRefreshHomeData));
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        mViewModel.initData();
        return view;

    }

    @Override
    public void onDetach() {
        super.onDetach();
        if (getActivity() != null) {
            getActivity().unregisterReceiver(refreshHomeDataReceiver);
        }
    }


    @Override
    public void onPause() {
        super.onPause();
        binding.layoutSchedule.scheduleNoticeview.pause();
    }

    @Override
    public void onResume() {
        super.onResume();
        //TODO 埋点
        ClickUtils.sendEvent(ClickApi.Home.page_show, getActivity(), "首页载入");
        mViewModel.getAgencyMatterStatisticsListData();
        refreshHomeData();
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }

    public void toDrugListOTO(){
        mViewModel.toDrugListOTO();
    }

    public void refreshHomeData() {
        mViewModel.getSecretWord();
        mViewModel.getHomePageBannerData();
        mViewModel.getHomeCommodityGroupList();
        if (ProjectConfigure.Open.isOpenLive) {
            mViewModel.getLiveTab();
        }
    }
}
